package pkg.andru.animation;

import java.util.ArrayList;
import java.util.List;

import pkg.andru.animation.Frame.FrameDrawState;
import pkg.andru.animation.Sprite.SpriteDrawState;
import android.graphics.Canvas;
import android.util.Log;

public class Frame implements IDrawable<FrameDrawState> {

    static final String TAG = "Frame";

    private int id;
    private String name;

    private List<SpriteWapper> mSprites = null;

    public Frame() {
        mSprites = new ArrayList<SpriteWapper>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addSprite(Sprite sprite, Sprite.SpriteDrawState state) {
        final SpriteWapper wapper = new SpriteWapper();
        wapper.sprite = sprite;
        wapper.drawState = state;

        mSprites.add(wapper);
    }

    public Sprite getSprite(int position) {
        return mSprites.get(position).sprite;
    }

    public int getSpriteCount() {
        return mSprites.size();
    }

    boolean isBlankFrame() {
        return mSprites.isEmpty();
    }

    class SpriteWapper {
        Sprite sprite;
        Sprite.SpriteDrawState drawState;

        @Override
        public String toString() {
            return "SpriteWapper [sprite=" + sprite.toString() + ", drawState=" + drawState.toString() + "]";
        }
    }

    @Override
    public String toString() {
        return "Frame [id=" + id + ", name=" + name + ", sprites count:" + getSpriteCount() + ", mSprites=" + mSprites + "]";
    }

    public static class FrameDrawState implements IDrawState {
        final int frameId;
        final float mX, mY, alpha;
        final int duration;
        float px, py;
        boolean keeped;

        public FrameDrawState(int id, float x, float y, int du, float a, boolean k) {
            frameId = id;
            mX = px = x;
            mY = py = y;
            duration = du;
            alpha = a;
            keeped = k;
        }

        public void updatePosition(float x, float y) {
            px = mX + x;
            py = mY + y;
        }

        public float getX() {
            return px;
        }

        public float getY() {
            return py;
        }

        public int getDuration() {
            return duration;
        }

        public boolean isKeeped() {
            return keeped;
        }

        @Override
        public String toString() {
            return "FrameDrawState [frameId=" + frameId + ", mX=" + mX + ", mY=" + mY + ", alpha=" + alpha + ", duration=" + duration + ", px=" + px + ", py=" + py + ", keeped=" + keeped + "]";
        }

    }

    @Override
    public void onDraw(Canvas canvas, FrameDrawState state) {
        for (SpriteWapper wapper : mSprites) {
            // update state
            final SpriteDrawState s = wapper.drawState;
            s.updateAlpha(state.alpha);
            s.updatePosition(state.getX(), state.getY());

            Log.i(TAG, "frame in ondraw:" + state.getX() + "," + state.getY());
            wapper.sprite.onDraw(canvas, s);
        }
    }
}
